<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../layui/css/layui.css"  media="all">

  <style>
  html{
  padding:10px;
  }
  body{
  overflow-y:scroll;
  }
    .cate_list {
      list-style: none;
      padding: 10px;
    }

    .cate_list .cate_item {
      margin: 2px;
      height: 36px;
      line-height: 36px;
      padding: 0 15px;
      background-color: #009688;
      color: #f2f2f2;
      border-radius: 2px;
      font-size: 14px;
    }
  </style>
</head>
<body>
           
<button class="layui-btn layui-btn-sm layui-btn-normal" id="btnAdd" >添加分类</button>
<button class="layui-btn layui-btn-sm" id="btnSort" >分类排序</button>

<table class="layui-hide" id="cate" lay-filter="cate"></table>

<ul class="cate_list" id="cate_sortable" style="display: none;">
  <li class="cate_item">1 java</li>
  <li class="cate_item">2 c++</li>
  <li class="cate_item">3 合集模式</li>
  <li class="cate_item">4 算法基础</li>
  <li class="cate_item">4 算法基础</li>
</ul>

<div id="categories-form" style="display: none;padding:20px;"> 
  <label >分类名</label>
  <div class="layui-input-inline">
    <input type="text" name="categoryName" class="layui-input">
  </div>
</div>
<!-- 添加删除按钮 -->
<div type="text/html" id="barDemo" style="display: none;">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</div>


<script src="../layui/layui.js" charset="utf-8"></script>
<script src="../vendor/html5sortable.min.js"></script>


<script>

sortable('#cate_sortable', {
  forcePlaceholderSize: true,
  placeholderClass: 'mb1 bg-navy border border-yellow',
  hoverClass: 'bg-maroon yellow'
});


layui.use(['table', 'layer'], function() {
  var table = layui.table;

  table.render({
    elem: '#cate',
    id:'table_category',
    url:'../category/findAll',
    cols: [[ //标题栏
      {checkbox: true},
      {field: 'id', title: 'ID', width: 100},
      {field: 'priority', title: '优先级', width: 120, edit: 'text'},
      {field: 'name', title: '名字', minwidth: 200, edit: 'text'},
      //删除按钮
      {title:'操作', width:200, toolbar:'#barDemo'}
    ]],
  
  });

  //监听单元格编辑
  table.on('edit(cate)', function(elem) {
    var field = elem.field; //得到字段
    var value = elem.value; //得到修改后的值
    var data = elem.data; //得到所在行所有键值
	
    //创建一个新的对象
    var category={};
    category.id=elem.data.id;
    //给对象动态的添加一个属性，属性的名字存在一个变量里面
    //直接写category.field的话在category对象里面会显示{id: 1, field: 123}
    category[field]= value;
    
    //发一个ajax 的post请求
    $.post("../category/update",category,function(data){
    	layer.msg("修改成功")
    })
    
    layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
    
  });
  
  
  
  //监听工具条
  table.on('tool(cate)', function(obj){
    var data = obj.data;
//    console.log(data);
    if(obj.event === 'del'){
    	layer.confirm('真的删除行么', function(index){
         //   obj.del();
         //发送一个ajax请求，把要删除的Id传过去
         var name=data.id;
         $.get("../category/delete?id="+name,function(data){
        	 //layer.msg("删除成功");
        	 if(data.code==0){
        		 layer.msg("删除成功");
        		 table.reload('category_table');
        	 }else{
        		 layer.msg(data.msg);
        	 }
     
         })
            layer.close(index);
    	});
    }

  });
 

  var $ = layui.jquery;
  
  $('#btnAdd').on('click', function() {
	layer.open({
		type: 1
		,title: '添加分类'
		,closeBtn: false
		,shade: 0.0
		,id: 'categories-add-form' //设定一个id，防止重复弹出
		,btn: ['保存', '关闭']
		,btnAlign: 'r'
		,moveType: 1 //拖拽模式，0或者1
		,content: $('#categories-form')
		,success: function(layero){
		  var btn = layero.find('.layui-layer-btn');
		  btn.find('.layui-layer-btn0').on('click', function() {
			var name = layero.find('input[name="categoryName"]').val();
			//点击按钮发送ajax请求,吧分类名称传到后台,后台往数据库里面添加一个分类
			$.get("../category/insert",{"name":name},function(data){
				console.log("天");
				table.reload('table_category');
			})
			
		  });
		}
	  }); 
  });
  
  $('#btnSort').on('click', function(){
    layer.open({
        type: 1
        ,title: '拖拽分类来重新排序'
        ,closeBtn: false
        ,area: '250px;'
        ,shade: 0.0
        ,id: 'category_sort' //设定一个id，防止重复弹出
        ,btn: ['保存', '关闭']
        ,btnAlign: 'r'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: $('#cate_sortable')
        ,success: function(layero){
          var btn = layero.find('.layui-layer-btn');
          btn.find('.layui-layer-btn0').on('click', function() {
        	  alert(name);
          });
        }
      });
  });
});
</script>

</body>
</html>